using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Omniture.API.Generators
{
	internal abstract class BaseTemplate : Widgetsphere.GeneratorEngine.FileTemplate
	{
		private OmnitureProject _project;

		protected BaseTemplate(OmnitureProject project)
		{
			_project = project;
		}

		protected internal OmnitureProject Project
		{
			get { return _project; }
		}

		public static string GetProjectName(OmnitureProject project)
		{
			return "Omniture." + project.CompanyName + "." + project.ProjectName;
		}

		public string ProjectName
		{
			get { return "Omniture." + _project.CompanyName + "." + _project.ProjectName; }
		}

		public override void Generate(string destinationFolder)
		{
			if (!Directory.Exists(destinationFolder))
				Directory.CreateDirectory(destinationFolder);

			destinationFolder = Path.Combine(destinationFolder, this.ProjectName);

			if (!Directory.Exists(destinationFolder))
				Directory.CreateDirectory(destinationFolder);

			string fileName = Path.Combine(destinationFolder, this.FileName);
			FileInfo fi = new FileInfo(fileName);

			if (!Directory.Exists(fi.DirectoryName))
				Directory.CreateDirectory(fi.DirectoryName);

			StreamWriter sw = File.CreateText(Path.Combine(destinationFolder, this.FileName));
			sw.Write(this.FileContent);
			sw.Close();
		}

		#region ITemplate Members
		public override abstract string FileContent { get;}
		public override abstract string FileName { get;}
		#endregion
	}
}
